package com.by.controller;

import com.by.bean.Dic;
import com.by.bean.PageBean;
import com.by.bean.ResultBean;
import com.by.group.ISave;
import com.by.group.V;
import com.by.query.DicQuery;
import com.by.service.DicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * date 2025/2/24 16:11
 * author bjj
 * decoration
 * version 1.0
 * since jdk11
 */
@RestController
@RequestMapping("/api")
@Api(tags = "字典管理")
public class DicController extends BaseController{

    @Autowired
    DicService dicService;

    /**
     * 根据条件查询
     * @param dicQuery
     * @return
     */
    @ApiOperation("根据条件查询")
    @PostMapping("/dic/query")
    public ResultBean query(@RequestBody DicQuery dicQuery){
       PageBean pageBean = dicService.query(dicQuery);
       return success("查询成功",pageBean);
    }

    /**
     * 新增字典
     * @param dic
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code",value = "字典编码",required = true),
            @ApiImplicitParam(name = "name",value = "字典名称",required = true),
            @ApiImplicitParam(name = "parentId",value = "字典类型",required = true)
    })
    @ApiOperation("新增字典")
    @PostMapping("/dic/save")
    public ResultBean save(@Validated(ISave.class) @RequestBody Dic dic, BindingResult result){
        V.validate(result);
        int rs = dicService.save(dic);
        return saveResult(rs);
    }

    /**
     * 更新字典
     * @param dic
     * @return
     */
    @ApiOperation("更新字典")
    @PostMapping("/dic/update")
    public ResultBean update(@RequestBody Dic dic){
        int rs = dicService.update(dic);
        return updateResult(rs);
    }

    /**
     * 删除字典
     * @param code
     * @return
     */
    @ApiOperation("删除字典")
    @DeleteMapping("/dic/delete")
    public ResultBean delete(int code){
        int rs = dicService.delete(code);
        return deleteResult(rs);
    }

    /**
     *  根据pid查询所有字典
     * @param pid
     * @return
     */
    @ApiImplicitParam(name = "pid",value = "字典类型",required = true)
    @ApiOperation("根据pid查询所有字典")
    @GetMapping("/dic/queryById")
    public ResultBean queryById(@RequestParam int pid){
        List<Map> maps = dicService.queryById(pid);
        return success("查询成功",maps);
    }
}
